package com.qf.agro.service.facilities.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.agro.facilities.Facilities;
import com.qf.agro.facilities.facilitiesConfig;
import com.qf.agro.facilities.facilitiesLabelValue;
import com.qf.agro.mapper.FacilitiesMapper;
import com.qf.agro.service.facilities.facilitiesConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qf.agro.mapper.facilitiesConfigMapper;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
public class facilitiesConfigServiceImpl implements facilitiesConfigService {
   @Autowired
    private facilitiesConfigMapper facilitiesConfigMapper;
   @Autowired
   private FacilitiesMapper facilitiesMapper;

private String label;
private Long value;
    @Override
    public Page<facilitiesConfig> findPage(Integer pageNum, Integer pageSize, String name, Long facilitiesId) {
        LambdaQueryWrapper<facilitiesConfig> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(name!=null,facilitiesConfig::getFacilitiesName,name)
                .eq(facilitiesId!=null,facilitiesConfig::getFacilitiesId,facilitiesId);
        Page<facilitiesConfig> page=new Page<>(pageNum,pageSize);
      page=  facilitiesConfigMapper.selectPage(page,queryWrapper);
      return page;
    }
    @Override
    public void save(facilitiesConfig facilitiesConfig) {
        facilitiesConfigMapper.insert(facilitiesConfig);
    }
    @Override
    public void delete(Long id) {
           facilitiesConfigMapper.deleteById(id);
    }
    @Override
    public void edit(facilitiesConfig facilitiesConfig) {
        facilitiesConfigMapper.updateById(facilitiesConfig);
    }
    @Override
    public List<facilitiesLabelValue> findValueLabel() {
        LambdaQueryWrapper<Facilities> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.select(Facilities::getId,Facilities::getFacilitiesName);
        List<Facilities> facilities = facilitiesMapper.selectList(queryWrapper);
        List<facilitiesLabelValue> list=new ArrayList<>();
        Facilities facilities1=new Facilities();


        Iterator i=facilities.iterator();
        while (i.hasNext()){
            facilities1=(Facilities) i.next();
            Long facilitiesId = facilities1.getId();
            String facilitiesName = facilities1.getFacilitiesName();
            facilitiesLabelValue facilitiesLabelValue=new facilitiesLabelValue();
            facilitiesLabelValue.setLabel(facilitiesName);
            facilitiesLabelValue.setValue(facilitiesId);
          list.add(facilitiesLabelValue);
        }
        System.out.println(list);
        return list;
    }
}
